package com.example.wangning.mycarofcountry.login;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.wangning.mycarofcountry.R;

/**
 * Created by dllo on 16/1/20.
 */
public class LoginFragment extends Fragment implements View.OnClickListener, TextWatcher {
    private EditText loginNameTo_Ed, passwordTo_Ed;
    private Button login_btn;
    private ImageView qqLogin_Iv;
    private Intent intent;
    private SharedPreferences sp;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_login,container,false);

        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        super.onViewCreated(view, savedInstanceState);
        loginNameTo_Ed = (EditText) view.findViewById(R.id.loginNameTo_Ed);
        passwordTo_Ed = (EditText) view.findViewById(R.id.passwordTo_Ed);
        login_btn = (Button) view.findViewById(R.id.login_btn);
        qqLogin_Iv = (ImageView) view.findViewById(R.id.qqLogin_Iv);
        qqLogin_Iv.setOnClickListener(this);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        login_btn.setOnClickListener(this);
        loginNameTo_Ed.addTextChangedListener(this);
        passwordTo_Ed.addTextChangedListener(this);
        qqLogin_Iv.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        intent = new Intent();
        switch (v.getId()){
            case R.id.login_btn:
                sp = getActivity().getSharedPreferences("word", Context.MODE_PRIVATE);
                String s = sp.getString("password","123");
                if (passwordTo_Ed.getText().toString().compareTo(s) == 0){
                    intent.putExtra("loginName", loginNameTo_Ed.getText().toString());
                    getActivity().setResult(200, intent);
                    getActivity().finish();
                } else {
                    Toast.makeText(getActivity(), "用户名或密码错误", Toast.LENGTH_LONG).show();
                }
                break;
        }

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        if (loginNameTo_Ed.getText().toString() != null &&
                loginNameTo_Ed.getText().toString().length() > 0
                && passwordTo_Ed.getText().toString() != null &&
                passwordTo_Ed.getText().toString().length() > 0) {
            login_btn.setEnabled(true);
            login_btn.setBackgroundResource(R.color.blue_color);
        }
    }
}
